package view;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PreferencesPanel extends javax.swing.JPanel implements ActionListener, ChangeListener, MouseListener {

	private static final long serialVersionUID = 1L;
	private JPanel tabLibLocPanel;
	private JTextField tabLibPathTextField;
	private JPanel fontPanel;
	private JComboBox fontSizeComboBox;
	private JLabel fontSizeLabel;
	private JPanel fontExamplePanel;
	private JTextField backgroundColorTextField;
	private JTextField foregroundColorTextField;
	private JTextArea fontPreviewTextArea;
	private JLabel bgColorLabel;
	private JLabel fontColorLabel;
	private JLabel fontFaceLabel;
	private JComboBox fontComboBox;
	private JButton applyButton;
	private JButton browseButton;
	private View parent;
	private boolean isLoading = true;
	private JColorChooser tcc;
	private JTextField colorChange;

	public PreferencesPanel(View p) {
		super();
		parent = p;
		initGUI();
		isLoading = false;
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(768, 439));
			this.setLayout(null);
			{
				tabLibLocPanel = new JPanel();
				this.add(tabLibLocPanel);
				tabLibLocPanel.setBounds(12, 18, 563, 47);
				tabLibLocPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED), "Tab & Library Folder", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION));
				tabLibLocPanel.setLayout(null);
				{
					
					File f = new File(parent.getTabRootPath());					
					tabLibPathTextField = new JTextField();
					tabLibLocPanel.add(tabLibPathTextField);
					tabLibPathTextField.setText(f.getCanonicalPath());
					tabLibPathTextField.setBounds(12, 21, 438, 17);
				}
				{
					browseButton = new JButton();
					tabLibLocPanel.add(browseButton);
					browseButton.setText("Browse");
					browseButton.setBounds(456, 21, 95, 17);
				}
			}
			{
				applyButton = new JButton();
				this.add(applyButton);
				applyButton.setText("Apply");
				applyButton.setBounds(194, 370, 267, 41);
				applyButton.setActionCommand("applyButton");
				applyButton.addActionListener(this);
			}
			{
				fontPanel = new JPanel();
				this.add(fontPanel);
				fontPanel.setBounds(12, 70, 563, 278);
				fontPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED), "Tab Display Options", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION));
				fontPanel.setLayout(null);
				{
					ComboBoxModel fontComboBoxModel = new DefaultComboBoxModel();
					fontComboBox = new JComboBox();
					fontPanel.add(fontComboBox);
					fontComboBox.setModel(fontComboBoxModel);
					fontComboBox.setBounds(56, 20, 194, 18);
					fontComboBox.addActionListener(this);
					fontComboBox.setActionCommand("fontFace");
					populateComboBoxWithFonts(fontComboBox);
					fontComboBoxModel.setSelectedItem(parent.getPreferredFont().getName());
				}
				{
					fontFaceLabel = new JLabel();
					fontPanel.add(fontFaceLabel);
					fontFaceLabel.setText("Face:");
					fontFaceLabel.setBounds(13, 22, 43, 15);
				}
				{
					fontSizeLabel = new JLabel();
					fontPanel.add(fontSizeLabel);
					fontSizeLabel.setText("Size:");
					fontSizeLabel.setBounds(12, 47, 42, 15);
				}
				{
					ComboBoxModel fontSizeComboBoxModel = 
							new DefaultComboBoxModel(new Integer[] { 8, 9, 10, 11, 12, 14, 16, 18, 20 });
					fontSizeComboBox = new JComboBox();
					fontPanel.add(fontSizeComboBox);
					fontSizeComboBox.setModel(fontSizeComboBoxModel);
					fontSizeComboBox.setBounds(56, 45, 74, 20);
					fontSizeComboBox.addActionListener(this);
					fontSizeComboBox.setActionCommand("fontSize");
					fontSizeComboBox.setSize(74, 18);
					fontSizeComboBoxModel.setSelectedItem(parent.getPreferredFont().getSize());
				}
				{
					fontColorLabel = new JLabel();
					fontPanel.add(fontColorLabel);
					fontColorLabel.setText("Font Color:");
					fontColorLabel.setBounds(12, 71, 118, 15);
				}
				{
					bgColorLabel = new JLabel();
					fontPanel.add(bgColorLabel);
					bgColorLabel.setText("Backgound Color:");
					bgColorLabel.setBounds(12, 94, 147, 15);
				}
				{
					fontExamplePanel = new JPanel();
					fontPanel.add(fontExamplePanel);
					fontExamplePanel.setBounds(262, 21, 288, 184);
					fontExamplePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED), "Font Preview", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION));
					fontExamplePanel.setLayout(null);
					{
						fontPreviewTextArea = new JTextArea();
						fontExamplePanel.add(fontPreviewTextArea);
						fontPreviewTextArea.setBounds(12, 21, 263, 150);
						fontPreviewTextArea.setEditable(false);
						fontPreviewTextArea.setFont(parent.getPreferredFont());
						fontPreviewTextArea.setForeground(parent.getPrefFGColor());
						fontPreviewTextArea.setBackground(parent.getPrefBGColor());
						fontPreviewTextArea.setText("The Quick Brown Fox\n" + 
													"Took A Wicked Diggah\n" + 
													"e|-------4---5---6-------|\n" + 
													"B|---------2-5------3----|\n" + 
													"G|-----------------------|\n" + 
													"D|---------2/5------3----|\n" + 
													"A|--------------x--------|\n" + 
													"E|---------2/5--x---3----|\n");
						fontPreviewTextArea.setMargin(new java.awt.Insets(10, 10, 2, 1));
					}
				}
				{
					foregroundColorTextField = new JTextField();
					fontPanel.add(foregroundColorTextField);
					foregroundColorTextField.setBounds(157, 69, 21, 19);
					foregroundColorTextField.setEditable(false);
					foregroundColorTextField.addMouseListener(this);
					foregroundColorTextField.setBackground(parent.getPrefFGColor());
				}
				{
					backgroundColorTextField = new JTextField();
					fontPanel.add(backgroundColorTextField);
					backgroundColorTextField.setBounds(157, 92, 21, 19);
					backgroundColorTextField.setEditable(false);
					backgroundColorTextField.addMouseListener(this);
					backgroundColorTextField.setBackground(parent.getPrefBGColor());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void populateComboBoxWithFonts(JComboBox box)	{
	    String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

	    for(int x=0; x < fonts.length; x++)
	    	box.addItem(fonts[x]);
	}
	
	public void createNewFont()	{
		Integer fs = (Integer)fontSizeComboBox.getSelectedItem();
		String face = (String)fontComboBox.getSelectedItem();
		Font f = new Font(face, Font.BOLD, fs);
		parent.setPreferredFont(f);
		parent.savePreferences();
		fontPreviewTextArea.setFont(f);
	}

	public void showColorPicker()	{
		JLabel jt = new JLabel("||||||||||||||||||||||||||||||||||||||");
		jt.setSize(jt.getPreferredSize());
		jt.setBorder(BorderFactory.createEmptyBorder(0,0,1,0));
		jt.setBackground(Color.white);
		tcc = new JColorChooser(colorChange.getBackground());
		tcc.getSelectionModel().addChangeListener(this);
		tcc.setBorder(BorderFactory.createTitledBorder("Select A Color"));
		tcc.setPreviewPanel(jt);
		JFrame temp = new JFrame();
		temp.add(tcc);
		temp.pack();
		temp.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if( isLoading )
			return;
		
		if( arg0.getActionCommand().equals("applyButton") )	{
			parent.savePreferences();
			JOptionPane.showMessageDialog(this, "You must reload any tabs you have open\n" +
												"for the changes to take affect.");
		}
		else if( arg0.getActionCommand().equals("fontSize") )	{
			createNewFont();
		}
		else if( arg0.getActionCommand().equals("fontFace") )	{
			createNewFont();
		}
		else if( arg0.getActionCommand().equals("fgColor") )	{
			colorChange = foregroundColorTextField;
			showColorPicker();
		}
		else if( arg0.getActionCommand().equals("bgColor") )	{
			colorChange = backgroundColorTextField;
			showColorPicker();
		}
		
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
        Color newColor = tcc.getColor();
        colorChange.setBackground(newColor);

        Color fg = foregroundColorTextField.getBackground();
        Color bg = backgroundColorTextField.getBackground();

        parent.setPrefFGColor(fg);
        parent.setPrefBGColor(bg);
    
        // Update view
        fontPreviewTextArea.setForeground(fg);
        fontPreviewTextArea.setBackground(bg);
        fontPreviewTextArea.repaint();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		colorChange = (JTextField)arg0.getSource();
		showColorPicker();
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}	
}
